home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / include / set.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-07  |  3.0 KB  |  138 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1986, 1988 Regents of the University of California
  5.    Copyright (c) 1989, Tera Computer Company
  6.  **/
  7.  
  8.   /* set.h -- definitions for sets */
  9.  
  10. #ifndef set_h
  11. #define set_h
  12.  
  13. #include "attribute.h"
  14.  
  15. #define    MAXNODES    2000    /* maximum number of nodes */
  16. #define    BITS_PER_CHAR    8    /* number of bits in a char */
  17. #define MAXBYTES    (1 + (MAXNODES / BITS_PER_CHAR))
  18.  
  19. struct set
  20. {
  21.    int first, last;
  22.    char bytes[MAXBYTES];
  23. };
  24.  
  25. typedef struct set SET;
  26.  
  27. #ifndef MAX
  28. #define MAX(A,B)    ( (A) > (B) ? (A) : (B) )
  29. #endif
  30.  
  31. #ifndef MIN
  32. #define MIN(A,B)    ( (A) < (B) ? (A) : (B) )
  33. #endif
  34.  
  35. #define    byte(n)        (n >> 3)    /* byte for element n */
  36. #define    bit(n)        (n & 07)    /* bit in byte for element n */
  37.  
  38. #define    in(set, n)    (((set)->bytes[byte(n)] >> bit(n)) & 01)
  39.  
  40. #define adjust_set_limits(set)    \
  41.     { int i;\
  42.       for (i = (set)->first; i < (set)->last + 1; i++)\
  43.       {\
  44.           (set)->first = i;\
  45.           if (in(set,i))\
  46.               break;\
  47.       }\
  48.       for (i = (set)->last; i > (set)->first - 1; i--)\
  49.       {\
  50.           (set)->last = i;\
  51.           if (in(set,i))\
  52.               break;\
  53.       }\
  54.       if (!in(set, (set)->first))\
  55.       {   (set)->first = MAXNODES - 1;\
  56.           (set)->last = 0;\
  57.       }\
  58.     }
  59.  
  60. #define    add_element(set, n)    \
  61.     {\
  62.        (set)->bytes[byte(n)] |= (1 << bit(n));\
  63.        (set)->first = MIN((set)->first, n);\
  64.        (set)->last = MAX((set)->last, n);\
  65.     }
  66.  
  67. #define    remove_element(set, n)    \
  68.     {\
  69.        (set)->bytes[byte(n)] &= ~(1 << bit(n));\
  70.        if (n == (set)->first || n ==(set)->last) \
  71.           adjust_set_limits(set);\
  72.     }
  73. #define    clear_set(set)        \
  74.     { int i;\
  75.       for (i = 0; i < MAXBYTES; i++)\
  76.          (set)->bytes[i] = 0;\
  77.       (set)->first = MAXNODES - 1;\
  78.       (set)->last = 0;\
  79.     }
  80.  
  81. #define    init_set(set)        { new(set, SET); clear_set(set); }
  82.  
  83. #define    copy_set(set2, set1)    \
  84.     { int i;\
  85.       for (i = 0; i < MAXBYTES; i++)\
  86.           (set2)->bytes[i] = (set1)->bytes[i];\
  87.       (set2)->first = (set1)->first;\
  88.       (set2)->last = (set1)->last;\
  89.     }
  90.  
  91. #define    each_element(set, vno)    \
  92.     { for (vno = (set)->first; vno < (set)->last + 1; vno++)\
  93.       if (in(set, vno))\
  94.       {
  95.  
  96. #define    union(set1, set2)    \
  97.     { int i;\
  98.       (set1)->first = MIN((set1)->first,(set2)->first);\
  99.       (set1)->last = MAX((set1)->last,(set2)->last);\
  100.       for (i = 0; i < MAXBYTES; i++)\
  101.           (set1)->bytes[i] |= (set2)->bytes[i];\
  102.     }
  103.  
  104. #define    intersect(set1, set2)    \
  105.     {\
  106.       (set1)->first = MAX((set1)->first,(set2)->first);\
  107.       (set1)->last = MIN((set1)->last,(set2)->last);\
  108.       { int i;\
  109.         for (i = 0; i < MAXBYTES; i++)\
  110.             (set1)->bytes[i] &= (set2)->bytes[i];\
  111.         adjust_set_limits(set1);\
  112.       }\
  113.     }
  114.  
  115. #define    difference(set1, set2)    \
  116.     { int i;\
  117.       for (i = 0; i < MAXBYTES; i++)\
  118.           (set1)->bytes[i] &= ~((set2)->bytes[i]);\
  119.       adjust_set_limits(set1);\
  120.     }
  121.  
  122. #define    xor(set1, set2)        \
  123.     {\
  124.       (set1)->first = MIN((set1)->first,(set2)->first);\
  125.       (set1)->last = MAX((set1)->last,(set2)->last);\
  126.       { int i;\
  127.         for (i = 0; i < MAXBYTES; i++)\
  128.            (set1)->bytes[i] = ((set1)->bytes[i] | (set2)->bytes[i]) &\
  129.            ~((set1)->bytes[i] & (set2)->bytes[i]);\
  130.         adjust_set_limits(set1);\
  131.       }\
  132.     }
  133.  
  134. BOOL empty();
  135. BOOL equal();
  136.  
  137. #endif
  138.